package jinyilw.tools.net.dev;

import java.lang.reflect.Array;

public class NetworkUtil
{
	private NetworkUtil()
	{
	}

	public static Object expand(Object src, int increase, boolean bottom)
	{
		int size = Array.getLength(src);
		Object dest = Array.newInstance(src.getClass().getComponentType(),
				size + increase);
		System.arraycopy(src, 0, dest, (bottom) ? 0 : increase, size);
		return dest;
	}

	public static Object expand(Object src, int increase)
	{
		return NetworkUtil.expand(src, increase, true);
	}

	public static Object expand(Object src, int increase, boolean bottom,
			Class<?> type)
	{
		if (src == null)
		{
			return Array.newInstance(type, 1);
		}
		return NetworkUtil.expand(src, increase, bottom);
	}

	public static Object cut(Object src, int position)
	{
		int size = Array.getLength(src);
		if (size == 1)
		{
			return Array.newInstance(src.getClass().getComponentType(), 0);
		}

		int numMoved = size - position - 1;
		if (numMoved > 0)
		{
			System.arraycopy(src, position + 1, src, position, numMoved);
		}
		size--;
		Object dest = Array.newInstance(src.getClass().getComponentType(),
				size);
		System.arraycopy(src, 0, dest, 0, size);
		return dest;
	}
}
